package cn.edu.scnu.Service.impl;

import cn.edu.scnu.Mapper.UserMapper;
import cn.edu.scnu.Service.UserService;
import cn.edu.scnu.entity.Return;
import cn.edu.scnu.entity.User;
import cn.edu.scnu.util.JwtUitl;
import cn.edu.scnu.util.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/** 本类用于实现用户登录、注册的业务功能
 * @author duit
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册功能
     * @param user
     * @return Return
     */
    @Override
    public Return register(User user) {
        //判断用户是否接收到
        if(user.getUsername()==null)return new Return().error("用户名为空!_!");
        //判断用户已存在
        User one = findByUserName(user.getUsername());
        if(one != null){
            return new Return().error("用户已存在");
        }
        //注册用户
        user.setPassword(MD5Util.md5(user.getPassword()));//密码加密md5算法
        user.setUserId(UUID.randomUUID().toString().replaceAll("-",""));//生成ID
        boolean flag = userMapper.insert(user) > 0;
        if(flag)return new Return().sussess(user);
        else return new Return().error("注册失败!_!");
    }

    /**
     * 登录功能
     * @param user
     * @return
     */
    @Override
    public Return login(User user) {
        //判断用户已存在
        User one = findByUserName(user.getUsername());
        if(one == null){
            return new Return().error("登陆失败，用户未注册!_!");
        }
        //密码是否相同
        if(!one.getPassword().equals(MD5Util.md5(user.getPassword())))
            return new Return().error("密码不一致，登录失败!_!");
        else{
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", user.getUsername());
            String token = JwtUitl.genToken(claims);
            return new Return().sussess(token);
        }
    }

    /**
     * 通过用户名找到用户
     * @param username
     * @return
     */
    @Override
    public User findByUserName(String username) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        User user = userMapper.selectOne(lqw);
        return user;
    }

    @Override
    public Return updateUser(String oldName, User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, oldName);
        boolean flag = userMapper.update(user, lqw)>0;
        User user1 = this.findByUserName(user.getUsername());
        if(flag){
            Map<String, Object> claims = new HashMap<>();
            claims.put("username", user1.getUsername());
            String token = JwtUitl.genToken(claims);
            return new Return().sussess(token);
        }else{
            return new Return().error("服务器异常，请重试");
        }
    }
}
